package com.finance.logistics.anlyzier;


import com.finance.base.constant.BaseConstants;
import com.finance.base.enums.FeeCommitStateEnum;
import com.finance.base.enums.StateEnum;
import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.report.bean.pojo.LogisticsExtend;
import com.finance.system.bean.pojo.LogisticsColumnDef;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by CBWL on 2017/4/27.
 */
@Component
public class DirectionHandler extends AbstractHandler {

    @Override
    @Resource(name="temperatureHandler")
    public void setNextHandler(AbstractHandler nextHandler){
        super.setNextHandler(nextHandler);
    }


    @Override
    public void doHandler(LogisticsData logisticsData) {
        this.logger.info("待解析运单信息：{}", net.sf.json.JSONObject.fromObject(logisticsData).toString());
        Integer direction = logisticsData.getDirection();
        LogisticsColumnDef logisticsColumnDef = this.logisticsMappingService.getByClasz(this.getClass().getCanonicalName());
        if(logisticsColumnDef == null){
            this.logger.error("logisticsColumnDef is null 未知的运单列映射[direction]：{}",this.getClass().getCanonicalName());
            String errorReason = "未知的运单列映射[direction]：{" + this.getClass().getCanonicalName() + "}";
            this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason,BaseConstants.ANALYZE_ERROR_TYPE_DIRECZTION);
        }else{
            int target = HandlerHelper.analyze(direction,logisticsColumnDef);
            if(target == -1){
                this.logger.error("未知的运单列映射[direction]{}", direction);
                String errorReason = "未知的运单列映射[direction]{" + direction + "}";
                this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason,BaseConstants.ANALYZE_ERROR_TYPE_DIRECZTION);
            }else{
                String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
                LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
                logisticsData2update.setDirection(Integer.valueOf(target));
                try {
                    this.logisticsDataService.update(logisticsData2update);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                LogisticsExtend logisticsExtend = new LogisticsExtend();
                    logisticsExtend.setWaybillNum(logisticsData.getWaybillNum());
                    logisticsExtend.setBusinessCode(logisticsData.getBusinessCode());
                    logisticsExtend.setState(StateEnum.Valid.getState());
                    logisticsExtend.setFeeCommitted(FeeCommitStateEnum.UNCOMMITTED.getState());
                    logisticsExtend.setDataValid(Privilege.B1.getPrivilegeString());
                    logisticsExtend.setPostmanNo(logisticsData.getPostmanNo());
                this.logisticsExtendService.add(logisticsData.getSignTime(),logisticsExtend);
                this.getNextHandler().doHandler(logisticsData);
            }
        }
    }

}
